﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using WordService.Business.IBLL;
using WordService.Business;
using System.IO;
using WordLib.Model;
using WordService.Utility;
using WordService.Model;

namespace WordLib.ImportUI
{
    public partial class FolderReaderPanel : UserControl
    {
        public FolderReaderPanel()
        {
            InitializeComponent();
            LoadForm();
        }

        private IFileBLL fileBLL = BLLManager.CreateFileBLL();
        private IArticleBLL articleBLL = BLLManager.CreateArticleBLL();
        private String filePath = String.Empty;

        private void LoadForm()
        {
            if (filePath.Equals(String.Empty))
            {
                this.btnLoadData.Enabled = false;
                this.chkIsComplete.Enabled = false;
                this.chkIsSame.Enabled = false;
            }
            else
            {
                this.btnLoadData.Enabled = true;
                this.chkIsComplete.Enabled = true;
                this.chkIsSame.Enabled = true;
            }
        }

        private void btnChooseFolder_Click(object sender, EventArgs e)
        {
            DialogResult result = this.fbdChooseFolder.ShowDialog();
            if (result == DialogResult.OK)
            {
                // 设置路径
                filePath = this.fbdChooseFolder.SelectedPath;
                AddItem(@"您选择的目录为：");
                AddItem(filePath);
                AddItem(@"正在读取该目录下的数据文件：");
                ExecuteLoad();
                DirectoryInfo directory = new DirectoryInfo(filePath);
                FileInfo[] files = directory.GetFiles("*.txt");
                AddItem("(总共" + this.dgvDataList.Rows.Count + "条数据 " + files.Length + "个文件)");
            }
        }

        // 清除输出
        private void AddItem(String InputValue)
        {
            this.lbConsole.Items.Add(InputValue);
            this.lbConsole.Update();
            this.lbConsole.SelectedIndex = lbConsole.Items.Count - 1;
        }

        private void btnLoadData_Click(object sender, EventArgs e)
        {
            try
            {
                IList<Article> articles = ViewUtil.GetCreateModelList(((IList<InputDataModel>)this.dgvDataList.DataSource));
                this.articleBLL.CreateArticles(articles);
                MessageBox.Show(this, "您的数据已导入！", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.dgvDataList.DataSource = null;
                this.dgvDataList.Rows.Clear();
                filePath = String.Empty;
                LoadForm();
            }
            catch (Exception)
            {
                MessageBox.Show(this, "数据导入错误，请联系管理员！", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void chkIsSame_CheckedChanged(object sender, EventArgs e)
        {
            if (!this.chkIsSame.Checked)
            {
                AddItem(@"过滤重复数据……");
            }
            ExecuteLoad();
            AddItem("(最终读取了" + this.dgvDataList.Rows.Count + "条数据)");
        }

        private void chkIsComplete_CheckedChanged(object sender, EventArgs e)
        {
            if (!this.chkIsComplete.Checked)
            {
                AddItem(@"过滤不完整数据……");
            }
            ExecuteLoad();
            AddItem("(最终读取了" + this.dgvDataList.Rows.Count + "条数据)");
        }

        private void ExecuteLoad()
        {
            // 获取选择目录下的所有txt文件
            DirectoryInfo directory = new DirectoryInfo(filePath);
            FileInfo[] files = directory.GetFiles("*.txt");
            IList<InputDataModel> dataList = new List<InputDataModel>();
            foreach (FileInfo file in files)
            {
                if (file.Exists)
                {
                    AddItem("" + file.FullName + "");
                    IList<InputDataModel> dlist = this.fileBLL.GetFileList(file);
                    ((List<InputDataModel>)dataList).AddRange(dlist);
                }
            }
            dataList = ViewFilter.ModelListFilter(dataList, this.chkIsComplete.Checked, this.chkIsSame.Checked);
            // 数据绑定
            this.dgvDataList.DataSource = dataList;
            this.dgvDataList.Update();
            LoadForm();
        }
    }
}
